package com.maxd.respository.selfGoods;


import com.maxd.model.Goods;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Repository
public interface GoodsJpaRepository extends JpaRepository<Goods, Long> {

    //条件查询带分页
    Page<Goods> findAll(Specification<Goods> specification, Pageable pageable);

    //不带分页
    List<Goods> findAll(Specification<Goods> specification);

    //商品浏览量增加
    @Modifying
    @Transactional
    @Query(value = "update Goods s set s.browse=s.browse+1 where s.id=:id")
    Integer browse(@Param("id") Long id);

    //根据店铺id统计商品数量
    @Query(value = "select count(s.id) from Goods s where s.storeId=:storeId")
    int findByStoreId(@Param("storeId") Long storeId);

    //根据店铺id统计浏览量
    @Query(value = "select sum(s.browse) from Goods s where s.storeId=:storeId")
    Integer findSumByStoreId(@Param("storeId") Long storeId);

    //上下架店铺所有商品
    @Modifying
    @Transactional
    @Query(value = "update Goods s set s.status=:status where s.storeId=:storeId")
    Integer updateStatusByStoreId(@Param("status") Integer status, @Param("storeId") Long storeId);
}
